﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace EasyFramework._CodeTip
{
    class Reflection
    {
        /// <summary>
        /// 根据程序集名称与类名创建类型实例
        /// </summary>
        /// <typeparam name="T">实例类型</typeparam>
        /// <param name="fullname">类完整名</param>
        /// <returns>创建的实例</returns>
        public object CreateInstance(string fullname)
        {
            if (!String.IsNullOrEmpty(fullname))
            {
                string[] s = fullname.Split(',');
                if (s.Length == 2)
                {
                    // tip 1
                    Assembly ass = Assembly.Load(s[1]);
                    if (ass != null)
                    {
                        return ass.CreateInstance(s[0]);
                    }
                }
            }
            throw new Exception("CreateInstance<T>::程序集不存在");
        }

        /// <summary>
        /// 创建并调用实例方法
        /// </summary>
        public void InvokeMethod()
        {
            object helper = CreateInstance("We7.CMS.ApplicationHelper,We7.CMS.Install");
            if (helper != null)
            {
                // tip 2
                MethodInfo method = helper.GetType().GetMethod("ResetApplication");
                if (method != null)
                {
                    // tip 3
                    method.Invoke(helper, null);
                }
            }
        }
    }
}
